﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace d39
{
    [Serializable]
    public struct Coordinate
    {
        public Coordinate(Vector v)
        {
            V = v;
        }
        public Coordinate(int x, int y)
            : this(new Vector(x, y)) { }

        public readonly Vector V;

        public int X { get { return V.X; } }
        public int Y { get { return V.Y; } }

        public static Coordinate Parse(string str, int start, out int end)
        {
            return new Coordinate(Vector.Parse(str, start, out end));
        }

        public override string ToString()
        {
            return V.ToString();
        }

        public static Coordinate ParseHumanReadable(string str)
        {
            var pos = str.IndexOfAny("-0123456789".ToCharArray(), 1);
            int y = int.Parse(str.Substring(pos));
            str = str.Substring(0, pos);
            str = str.Substring(0, pos - 1).ToUpper() + char.ToLower(str[pos - 1]);
            int end;
            int x = Base26.ParseInt(str.Replace('-', '_'), 0, out end);
            return new Coordinate(x, y);
        }
        public string ToHumanReadableString()
        {
            return Base26.IntToString(X).Replace('_', '-').ToUpper() + Y;
        }

        public static Coordinate operator +(Coordinate coord, Vector v)
        {
            return new Coordinate(coord.X + v.X, coord.Y + v.Y);
        }
        public static Coordinate operator -(Coordinate coord, Vector v)
        {
            return new Coordinate(coord.X - v.X, coord.Y - v.Y);
        }
        public static Vector operator -(Coordinate a, Coordinate b)
        {
            return new Vector(a.X - b.X, a.Y - b.Y);
        }
        public static bool operator ==(Coordinate a, Coordinate b)
        {
            return a.X == b.X && a.Y == b.Y;
        }
        public static bool operator !=(Coordinate a, Coordinate b)
        {
            return a.X != b.X || a.Y != b.Y;
        }
        public override bool Equals(object obj)
        {
            return obj is Coordinate && this == ((Coordinate)obj);
        }
        public override int GetHashCode()
        {
            return X.GetHashCode() ^ Y.GetHashCode();
        }
    }
}